package com.example.helloword.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class PagesInReadView extends androidx.appcompat.widget.AppCompatTextView {



    private List<String> pages_book;
    private int current_page = 0;
    private int max_char_number = 0;
    private int page_number = 0;
    private SwitchChapterListener switchChapterListener;

    public PagesInReadView(@NonNull Context context) {
        super(context);

//        setText(book);
//        ii();
//        pagesTobook(book);

    }

    public PagesInReadView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        setText(book);
//        ii();
//        pagesTobook(book);

    }

    public PagesInReadView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        setText(book);
//        ii();
//        pagesTobook(book);

    }

    public void ii() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("PagesInReadView", "onClick");
//                setText(current_book);

                invalidate();
            }
        });
    }

    private void calculateCharNumber(){
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int lineHeight = getLineHeight();
        float textSize = getTextSize();
        int v = (int)((measuredWidth + 1) / textSize);
        int i =(int)( measuredHeight /130 );
        max_char_number=v*i;
//        setMaxLines(i);
        Log.v("calculateCharNumber",measuredWidth+"---"+measuredHeight+"---"+textSize+"---"+lineHeight);


    }

    public void pagesTobook(String book, boolean isNext) {
        List<String> strings = new ArrayList<>();
        int end_count = 0;
//        strings.add("");
        if (max_char_number==0)
        calculateCharNumber();

        page_number = book.length() / max_char_number + 1;
        for (int i = 0; i < page_number; i++) {
            if (i == page_number - 1) {
                String substring = book.substring(end_count, end_count + book.length() % max_char_number);
                strings.add(substring);
                break;
            }
            String substring = book.substring(end_count, end_count + max_char_number);
            strings.add(substring);
            end_count += max_char_number;
        }


        if (isNext) {
            current_page = 0;
        } else {
            current_page = strings.size() - 1;
        }
        setText(strings.get(current_page));


        pages_book = strings;
        invalidate();
    }


    private void previousPage() {
        if (current_page == 0) {
            if (switchChapterListener != null) {
                switchChapterListener.SwitchChapter(SwitchChapterListener.PREVIOUS, false);
            }
        } else {
            current_page--;
            setText(pages_book.get(current_page));
            invalidate();
        }

    }


    private void nextPage() {
        if (current_page == page_number - 1) {
            if (switchChapterListener != null) {
//                current_page = 0;
                switchChapterListener.SwitchChapter(SwitchChapterListener.NEXT, true);
            }
        } else {
            current_page++;
            setText(pages_book.get(current_page));
            invalidate();
        }
    }

    public void setSwitchChapterListener(SwitchChapterListener switchChapterListener) {
        this.switchChapterListener = switchChapterListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float width = getMeasuredWidth();
//        Log.v("PagesInReadView",event.getX()+"");


        if (event.getAction() == MotionEvent.ACTION_UP) {
//            Log.v("PagesInReadView","onClick");

            if (event.getX() > width / 2 + 100) {
                nextPage();
                Log.v("PagesInReadView", "onClick_right");
            } else if (event.getX() < width / 2 - 100) {
                previousPage();
                Log.v("PagesInReadView", "onClick_left");

            } else {
                Log.v("PagesInReadView", "onClick_middle");

            }
        }

        return true;
    }

    public interface SwitchChapterListener {
        public final static int NEXT = 0;
        public final static int PREVIOUS = 1;

        public void SwitchChapter(int type, boolean isNext);
    }
}
